import * as api from './api';
import {PAGE} from '../constants';
import {conventTableCondition} from '../utils';
export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';
const getInitDataBegin = () => ({
    type: GET_INIT_DATA_BEGIN
});

const getInitDataSuccess = data => ({
    type: GET_INIT_DATA_SUCCESS,
    data
});
const getInitDataFail = () => ({
    type: GET_INIT_DATA_FAIL
});
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInitData().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        else
            dispatch(getInitDataFail());
        return res.ok;
    });
    return Promise.all([init]).then(values => values[0] && values[1]);
};

/*==============================查询action================================= */
//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});

//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL
});

/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';

const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});

const getListDataSuccess = (condition, data) => ({
    type: GET_LIST_DATA_SUCCESS,
    condition,
    data
});

const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});

//获取查询列表
export const getList = condition => dispatch => {
    dispatch(getListDataBegin());
    const queryCondition = (conventTableCondition(condition));
    const {beginDateTime, endDateTime, ...props} = queryCondition;
    if(beginDateTime && beginDateTime.length > 0) {
        props.beginBeginDateTime = beginDateTime[0];
        props.endBeginDateTime = beginDateTime[1];
    }
    if(endDateTime && endDateTime.length > 0) {
        props.beginEndDateTime = endDateTime[0];
        props.endEndDateTime = endDateTime[1];
    }
    return api.getList(props).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(condition, res.data));
        else
            dispatch(getListDataFail());
    });
};

/*=============================Table数据刷新================================== */
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const brands = getState().getIn(['page', 'domainData', 'init', 'brands']).toJS();
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const pageTableCondition = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    if(brands.length === 1)
        condition.brandId = brands[0].id;
    condition.pageSize = pageTableCondition.pageSize || PAGE.size;
    condition.sortedColumnKey = pageTableCondition.sortedColumnKey;
    condition.sortedOrder = pageTableCondition.sortedOrder;
    dispatch(getList(condition));
};

